package com.huilan.hgt.util;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @Author lplu
 * @date 2018/6/7
 */
public class StringUtil {

    /**
     * 1,2,3 字符串转List集合
     *
     * @param
     * @Author: lplu
     * @Date: 2018/6/7 15:47
     * @return:
     */
    public static List<String> getList(String id) {
        List<String> list = new ArrayList<String>();
        if (id == null) {
            return list;
        }
        String[] str = id.split(",");
        for (int i = 0; i < str.length; i++) {
            list.add(str[i]);
        }
        return list;
    }

    /**
     * 根据分隔符，分割字符串
     *
     * @param id
     * @param flag
     * @return
     */
    public static List<String> getList(String id, String flag) {
        List<String> list = new ArrayList<String>();
        if (id == null || flag == null || flag.isEmpty() || id.isEmpty()) {
            return list;
        }
        String[] str = id.split(flag);
        for (int i = 0; i < str.length; i++) {
            list.add(str[i]);
        }
        return list;
    }

    /**
     * 1,2,3 字符串转List集合
     *
     * @param
     * @Author: lplu
     * @Date: 2018-8-20 16:13:25
     * @return:
     */
    public static List<String> getListBysplit(String id, String spilt) {
        List<String> list = new ArrayList<String>();
        String[] str = id.split(spilt);
        for (int i = 0; i < str.length; i++) {
            list.add(str[i]);
        }
        return list;
    }

    /**
     * 16进制转换成为string类型字符串
     *
     * @param s      16进制字符串
     * @param coding 编码
     * @return
     */
    public static String hexStringToString(String s, String coding) {
        if (s == null || s.isEmpty()) {
            return null;
        }
        s = s.replace(" ", "");
        byte[] baKeyword = new byte[s.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            s = new String(baKeyword, coding);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return s;
    }

    /**
     * 获取UUID
     *
     * @return
     */
    public static String getUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    /**
     * 拼接字符串。处理为null或者为""的情况
     * 处理ID拼接时存在null值的情况
     * 例如
     * ""、和"456"拼接后为"456"
     * null和"456"拼接后为"123"
     * "123""和456"拼接后为"123,456"
     *
     * @param src
     * @param tar
     * @return
     */
    public static String linkString(String src, String tar) {
        String res = "";
        if (src != null && !src.isEmpty()) {
            res = src;
            if (tar != null && !tar.isEmpty()) {
                res += "," + tar;
            }
        } else {
            if (tar != null && !tar.isEmpty()) {
                res += tar;
            }
        }
        return res;
    }


    public  static String convertFileSize(long size) {
        long kb = 1024;
        long mb = kb * 1024;
        long gb = mb * 1024;

        if (size >= gb) {
            return String.format("%.1f GB", (float) size / gb);
        } else if (size >= mb) {
            float f = (float) size / mb;
            return String.format(f > 100 ? "%.0f MB" : "%.1f MB", f);
        } else if (size >= kb) {
            float f = (float) size / kb;
            return String.format(f > 100 ? "%.0f KB" : "%.1f KB", f);
        } else {
            return String.format("%d B", size);
        }
    }
}
